package dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pojo.Admin;
import pojo.PayRecord;
import pojo.PayRecord;
import pojo.PayRecord;
import utils.JDBCUtils;
import utils.SystemEnum;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class PayRecordDaoImpl implements PayRecordDao{

    //调换信息
    private Log log = LogFactory.getLog(getClass());
    @Override
    public ArrayList<PayRecord> findAll() {
        log.info(SystemEnum.BEGIN);
        ArrayList<PayRecord> list = new ArrayList<PayRecord>();
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM pay_record ";
            statement = con.prepareStatement(sql);
            rs = statement.executeQuery();
            while(rs.next()){
                PayRecord payRecord = new PayRecord(rs.getInt(1),rs.getInt(2),rs.getDate(3),rs.getBigDecimal(4),rs.getBoolean(5));
                list.add(payRecord);
            }
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement,rs);
            log.info(SystemEnum.END);
        }
        return list;
    }

    @Override
    public PayRecord findById(Integer payId) {
        log.info(SystemEnum.BEGIN);
        PayRecord payRecord = new PayRecord();
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM check_in_record WHERE stuId = ?";
            statement = con.prepareStatement(sql);
            statement.setInt(1,payId);
            rs = statement.executeQuery();
            while(rs.next()){
                payRecord.setPayId(rs.getInt(1));
                payRecord.setStuId(rs.getInt(2));
                payRecord.setPay_time(rs.getDate(3));
                payRecord.setPayment(rs.getBigDecimal(4));
                payRecord.setRemark(rs.getBoolean(5));
            }
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement,rs);
            log.info(SystemEnum.END);
        }
        return payRecord;
    }

    @Override
    public int insert(Connection con,PayRecord payRecord) throws SQLException {
        log.info(SystemEnum.BEGIN);
        PreparedStatement statement = null;
        PreparedStatement statement2 = null;
        int result = 0;
        try {
            String sql = "INSERT INTO pay_record VALUES (?,?,?,?,?)";
            statement = con.prepareStatement(sql);
            statement.setInt(1,payRecord.getPayId());
            statement.setInt(2,payRecord.getStuId());
            statement.setDate(3,new java.sql.Date(payRecord.getPay_time().getTime()));
            statement.setBigDecimal(4,payRecord.getPayment());
            statement.setBoolean(5,payRecord.getRemark());
            result = statement.executeUpdate();
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
            throw e;
        }finally {
            JDBCUtils.close(null,statement);
            log.info(SystemEnum.END);
        }
        return result;
    }

    @Override
    public int update(PayRecord payRecord) {
        log.info(SystemEnum.BEGIN);
        Connection con = null;
        PreparedStatement statement = null;
        PreparedStatement statement2 = null;
        int result = 0;
        try {
            con = JDBCUtils.getConnection();
            String sql = "UPDATE pay_record SET stuId = ?,pay_time = ?,payment=?,remark=? WHERE payId = ?";
            statement = con.prepareStatement(sql);
            statement.setInt(1,payRecord.getStuId());
            statement.setDate(2,new java.sql.Date(payRecord.getPay_time().getTime()));
            statement.setBigDecimal(3,payRecord.getPayment());
            statement.setBoolean(4,payRecord.getRemark());
            statement.setInt(5,payRecord.getPayId());
            result = statement.executeUpdate();
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement);
            log.info(SystemEnum.END);
        }
        return result;
    }

    @Override
    public int delete(Integer payId) {
        log.info(SystemEnum.BEGIN);
        Connection con = null;
        PreparedStatement statement = null;
        PreparedStatement statement2 = null;
        int result = 0;
        try {
            con = JDBCUtils.getConnection();
            String sql = "DELETE FROM pay_record  WHERE payId = ? ";
            statement = con.prepareStatement(sql);
            statement.setInt(1,payId);
            result = statement.executeUpdate();
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement);
            log.info(SystemEnum.END);
        }
        return result;
    }
}
